﻿@using Pay.DTO;
@{
    ViewBag.Title = "商品管理";
    Layout = "~/Views/Shared/_MallLayout.cshtml";
    int curStatus = 0;
    if (Request["curStatus"] != null && !string.IsNullOrEmpty(Request["curStatus"].ToString())) {
        curStatus = int.Parse(Request["curStatus"].ToString());
    }
    shop_info_dto shop_info = ViewBag.ShopInfo as shop_info_dto;
}
<style type="text/css">

    .mescroll {
        position: fixed;
        top: 0;
        bottom: 0;
        height: auto;
        width: 100%;
    }
</style>
<body class="page-index page-myShareView page-manage">
    <div class="mescroll" id="mescroll">

        <div class="mod-type mod-typeTab">
            <ul id="status">
                <li id="status_1" class="cur" value="1">销售中<i></i></li>
                <li id="status_2" value="2">已售尽<i></i></li>
                <li id="status_3" value="3">库存中<i></i></li>
                <li id="status_4" value="4">已失效<i></i></li>
            </ul>
        </div>
        <form action="javascript:void(0);">
            <div class="">
                <div class="mod-search mod-search-bg mod-search-normal">
                    <div class="searchBox bg-white flex" style="background:#fff;">
                        @*<a href="javascript:;" class="searchBtn">
                            <i class="icon-search"></i>
                        </a>*@
                        <div class="searchBtn flex-box">
                            <i class="icon-sc"></i>
                        </div>

                        @*<input id="serch_input" class="searchInput" type="search" placeholder="请输入需要搜索的商品">
                        <a id="search_btn" href="javascript:void(0);" class="searchNow cur fn30">搜索</a>*@
                        <input id="serch_input" class="searchInput fn30 flex-1" type="search" placeholder="请输入需要搜索的商品">
                        <a id="search_btn" href="javascript:void(0);" class="searchNow cur fn30">搜索</a>

                    </div>
                </div>
            </div>
        </form>

        <div class="mod-pro2 lt-mb">
            <ul id="product"></ul>
        </div>
    </div>
    <!-- 底部导航 -->
    <div class="lt-footNav">
        <ul>
            <li>
                <a class="shareBtn1 col-gray" href="javascript:addProduct();">新增分销商品</a>
            </li>
            <li>
                <a class="shareBtn2" href="javascript:toDistribution();">分销库选择商品分销</a>
            </li>
        </ul>
    </div>
    <script type="text/javascript">
        $(function () {
            var curStatus = Number('@curStatus');
            if (curStatus > 0) {
                setTimeout(function () { $("#status_" + curStatus).trigger('click'); }, 100);
            }
        })
    </script>
    <link href="~/Scripts/mescroll/mescroll.min.css" rel="stylesheet" />
    <script src="~/Scripts/mescroll/mescroll.min.js"></script>
    <script src="~/Scripts/token-common.js"></script>
    <script type="text/javascript">
        $(function () {
            var status = 1;
            var product_name = "";
            var mescroll = new MeScroll("mescroll", {
                down: {
                    auto: false,
                    callback: function (mescroll) {
                        mescroll.resetUpScroll(); //下拉刷新的回调,默认重置上拉加载列表为第一页
                    }
                },
                up: {
                    callback: getListData,
                    clearEmptyId: "product",
                    isBounce: false,
                    noMoreSize: 3,
                    htmlNodata: '<p class="upwarp-nodata">--没有更多数据了 --</p >',
                    empty: {
                        warpId: "product",
                        icon: "",
                        tip: "亲,没有您要找的商品~"

                    },
                    toTop: {
                        //src: "/Content/css/tokenmall/img/toup.png",
                        //html: " <img src='/Content/css/tokenmall/img/toup.png' />",
                        //offset: 500
                    }
                }
            });

            function getListData(page) {
                $.ajax({
                    type: 'post',
                    url: '/ProductManage/_ProductList',
                    data: { page: page.num, size: page.size, status: status, product_name: product_name },
                    success: function (response) {
                        var paged = $(response).siblings("page");
                        var rows = Number.parseInt(paged.attr("rows"));
                        var total = Number.parseInt(paged.attr("total_result"));
                        mescroll.endBySize(rows, total);
                        if (paged.length === 1) {
                            $("#product").append($(response));
                            $('#product page').remove();
                        }

                    },
                    error: function (e) {
                        //联网失败的回调,隐藏下拉刷新和上拉加载的状态
                        mescroll.endErr();
                    }
                });
            }

            $("ul#status").on("click", "li", function () {
                //只需要找到你点击的是哪个ul里面的就行
                $(this).addClass("cur").siblings().removeClass("cur");
                status = $(this).val();
                //重置列表数据
                mescroll.resetUpScroll();
                //隐藏回到顶部按钮
                mescroll.hideTopBtn();
            });

            $("#serch_input").blur(function () {
                product_name = $("#serch_input").val();
                //重置列表数据
                mescroll.resetUpScroll();
                //隐藏回到顶部按钮
                mescroll.hideTopBtn();
            });
            $('#serch_input').keydown(function (event) {
                if (event.keyCode == 13) {
                    product_name = $(this).val();
                    //重置列表数据
                    mescroll.resetUpScroll();
                    //隐藏回到顶部按钮
                    mescroll.hideTopBtn();
                }
            });

            $("#search_btn").click(function () {
                product_name = $("#serch_input").val();
                //重置列表数据
                mescroll.resetUpScroll();
                //隐藏回到顶部按钮
                mescroll.hideTopBtn();
            });

        });


        function addProduct() {
            var isShopDisabled = Number('@(shop_info.is_disabled == true ? 1 : 0)');
            if (isShopDisabled == 1) {
                layer.msg("您的店铺已被禁用");
                return false;
            }
            try {
                if (browser.versions.ios) {
                    window.webkit.messageHandlers.addGoodsAction.postMessage('');
                } else if (browser.versions.android) {
                    androidfuntion.addGoodsAction()
                }
            } catch (e) {
                layer.msg("请下载最新版本APP，添加商品");
            }
        }

        function toDistribution() {
            var isShopDisabled = Number('@(shop_info.is_disabled == true ? 1 : 0)');
            if (isShopDisabled == 1) {
                layer.msg("您的店铺已被禁用");
                return false;
            }
            window.location.href = '/ProductManage/Distribution';

        }
    </script>

    <script type="text/javascript">
        //上架
        $("#product").delegate(".putaway_btn", 'click', function () {
            var product_id = $(this).parent(".btnBox").attr("data-productId");
            var distribution_product_id = $(this).parent(".btnBox").attr("data-distributionProductId");
            var is_proprietary = $(this).parent(".btnBox").attr("data-isProprietary");
            updateStatus(product_id, distribution_product_id, is_proprietary, 1)
        });
        //下架
        $("#product").delegate(".unShelve_btn", 'click', function () {
            var product_id = $(this).parent(".btnBox").attr("data-productId");
            var distribution_product_id = $(this).parent(".btnBox").attr("data-distributionProductId");
            var is_proprietary = $(this).parent(".btnBox").attr("data-isProprietary");
            updateStatus(product_id, distribution_product_id, is_proprietary, 2)
        });
        //删除
        $("#product").delegate(".delete_btn", 'click', function () {
            var product_id = $(this).parent(".btnBox").attr("data-productId");
            var distribution_product_id = $(this).parent(".btnBox").attr("data-distributionProductId");
            var is_proprietary = $(this).parent(".btnBox").attr("data-isProprietary");
            updateStatus(product_id, distribution_product_id, is_proprietary, 3)
        });
        //编辑
        $("#product").delegate(".edit_btn", 'click', function () {
            var product_id = $(this).parent(".btnBox").attr("data-productId");
            var isapp = $(this).parent(".btnBox").attr("data-app");
            if (isapp == '0') {
                //layer.msg("请打开PC供应商端，编辑商品");
                //return;
            }
            //if (isapp == '1') {
            //    layer.msg("请打开PC供应商端，编辑商品");
            //} else {
            try {
                if (browser.versions.ios) {
                    window.webkit.messageHandlers.editShopGoodsAction.postMessage(product_id);
                } else if (browser.versions.android) {
                    androidfuntion.editShopGoodsAction(product_id);
                }
            } catch (e) {
                layer.msg("请下载最新版本APP，编辑商品");
            }
            // }
        });
        //显示审核未通过的原因
        $("#product").delegate(".cause", 'click', function () {
            var el = $(this);
            el.toggleClass('cur');
        });


        function updateStatus(product_id, distribution_product_id, is_proprietary, status) {
            var tip = '';
            if (status == 1)
                tip = '上架';
            else if (status == 2)
                tip = '下架';
            else if (status == 3)
                tip = '删除'
            swal({
                title: "您确定要" + tip + "该商品吗？",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "",
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                closeOnConfirm: false
            },
                function (isConfirm) {
                    if (isConfirm) {
                        $.ajax({
                            url: '/ProductManage/UpdateProductStatus', type: "post",
                            data: { product_id: product_id, distribution_product_id: distribution_product_id, is_proprietary: is_proprietary, status: status },
                            success: function (response) {
                                if (response.result_code == 1000) {
                                    layer.msg(response.result_msg);
                                    location.reload();
                                }
                                else {
                                    layer.msg(response.result_msg);
                                }

                            }
                        });
                    } else {
                        //layer.msg("操作失败");
                    }

                });
        }
    </script>
</body>